package edu.java.homework.hw03.ipj05;

import java.util.Arrays;

public class J05 {
    public static void main(String[] args) {
	// int[] arr= { 1, 2, 3, 4, 5, 2, 3, 5, 10, 21, 1, 3, 2, 15 };
//	int[] arr = { 12, 12, 10, 7, 5, 3, 2, 2, 0, -12, -19 };
//	int[] arr = {12};
	// int[] arr=null;
	int[] arr = {};
	System.out.println(Arrays.toString(arr));
	selectionSort(arr);
	System.out.println();
	System.out.println(Arrays.toString(arr));
    }

    /**
     * selectionSort algorithm - compare consecutively every element with the
     * others and find the current minimal element.&nbsp;After that swap the elemnts
     * 
     * @param array
     */
    public static void selectionSort(int[] array) {
	if (array == null) {
	    throw new IllegalArgumentException(
		    "The array is null pointed.Incorrect input.");
	}
	int length = array.length;
	for (int i = 0; i <= length - 2; i++) {
	    int minElemIndex = i;
	    for (int j = i + 1; j <= length - 1; j++) {
		if (array[minElemIndex] > array[j]) {
		    minElemIndex = j;
		}
	    }
	    int tmpElem = array[i];
	    array[i] = array[minElemIndex];
	    array[minElemIndex] = tmpElem;
	}
    }

}